package com.example.wxback.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wxback.dao.IWxUserDao;
import com.example.wxback.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxUserService extends ServiceImpl<IWxUserDao, UserInfo> {

    @Autowired
    private IWxUserDao wxUserDao;

    public int countUser(JSONObject jsonObject) {
        return wxUserDao.countUser(jsonObject);
    }

    public int countAdmin(JSONObject jsonObject) {
        return wxUserDao.countUser(jsonObject);
    }

    public List<UserInfo> getUserList(JSONObject jsonObject) {
        return wxUserDao.getUserList(jsonObject);
    }

    public UserInfo getUser(int id) {
        return wxUserDao.getUser(id);
    }

    /**
     * 通过字账号查询主控端
     * @param id
     * @return
     */
    public UserInfo getUserByChildId(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("child_id", id);
        return wxUserDao.selectOne(queryWrapper);
    }

    public UserInfo getUserByPhone(String phone) {
        return wxUserDao.getUserByPhone(phone);
    }

    public boolean insertUser(UserInfo userInfo) {
        return wxUserDao.insertUser(userInfo) > 0;
    }

    public boolean updateUser(UserInfo userInfo) {
        return wxUserDao.updateUser(userInfo) > 0;
    }

    public boolean enableUser(UserInfo userInfo) {
        return wxUserDao.enableUser(userInfo) > 0;
    }

    public boolean deleteUser(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return baseMapper.delete(queryWrapper) > 0;
    }

    public boolean deleteUsers(List<String> list) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", list);
        return baseMapper.delete(queryWrapper) > 0;
    }

    public UserInfo getMasterInfoByChildId(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("child_id", id);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 绑定激活码
     *
     * @param userInfo
     * @return
     */
    public boolean bindActivateCode(UserInfo userInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userInfo.getId());
        return wxUserDao.update(userInfo, queryWrapper) > 0;
    }

    /**
     * 绑定被控端账号
     *
     * @param userInfo
     * @return
     */
    public boolean bindChildPhone(UserInfo userInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userInfo.getId());
        return wxUserDao.update(userInfo, queryWrapper) > 0;
    }

    /**
     * 更新deviceToken
     * @param userInfo
     * @return
     */
    public boolean uploadDeviceToken(UserInfo userInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userInfo.getId());
        return wxUserDao.update(userInfo, queryWrapper) > 0;
    }

    /**
     * 修改管理员密码
     * @param userInfo
     * @return
     */
    public boolean updateAdminPassword(UserInfo userInfo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userInfo.getId());
        queryWrapper.eq("member_type", 1);
        return baseMapper.update(userInfo, queryWrapper) > 0;
    }
}
